Skip to content

Reduce per-entry overhead in Expand-Archive - #169

Merged
Steve Lee (SteveL-MSFT) merged 2 commits into
PowerShell:servicingfrom
azchohfi:expand-archive-per-entry-perf
Nov 20, 2026
Merged

Steve Lee (SteveL-MSFT) merged 2 commits into
PowerShell:servicingfrom
azchohfi:expand-archive-per-entry-perf

Conversation

@azchohfi

Copy link
Copy Markdown
Collaborator

PR Summary

Expand-Archive does some script work for every entry before it extracts it, and with many small files that work takes most of the time. This change makes it cheaper without changing what gets extracted or what -PassThru returns.

For each entry the loop used to

  • create a List[string] with New-Object in Get-WindowsArchiveEntryPathValidationResult (Windows only). New-Object with a generic type name searches the loaded assemblies on every call.
  • create the FileInfo with New-Object
  • call Test-Path -PathType Container on the parent folder. The FileSystem provider fixes the casing of the whole path on every call, which costs a few milliseconds per call for a nested folder on Linux.
  • trim the path segments through a ForEach-Object pipeline

Now it collects the segments with a foreach statement, casts the path to FileInfo, checks the parent folder with [System.IO.Directory]::Exists() and trims the segments with a foreach statement. The manifest still says PowerShellVersion="3.0", so I avoided ::new().

This addresses PowerShell/PowerShell#28471.

PR Context

Expand-Archive of a zip with 2,000 small files in 200 folders on Windows 11, time inside the command, patched module over the servicing module, both loaded from unsigned copies in the same pwsh. Two separate runs of paired passes, median ratio with a 95% interval.

run 1 run 2
0.57x [0.56, 0.58] 0.56x [0.55, 0.60]

That's about 2.8 s less per expansion. Those runs used an earlier version of this change written with ::new(). I ran this branch once more against servicing, 10 paired passes with the earlier version in the same run. On Windows this branch came out at 0.62x [0.53, 0.70], about 3.1 s less, and the earlier version at 0.60x. On Linux (WSL2, ext4) the same run gave 0.23x [0.22, 0.27], about 9.6 s less on a 13 s expansion, because Test-Path is most of the per-entry cost there. Linux was only measured in that one run.

The extracted files are byte-identical and -PassThru returns the same items in the same order, on both platforms.

The new tests expand an archive with nested folders, empty folder entries, a zero-byte file, binary content and Unicode names (Cyrillic, CJK, Greek and an emoji). They compare every file byte for byte and the exact -PassThru output, for a fresh expansion and for -Force over an existing one with a changed file and a deleted folder. There's also a test for a reserved device name in a nested entry, since the segment handling changed. They pass with and without this change, so they pin the current behaviour.

PowerShell 7.6.10 and 7.7.0 ship 1.2.7, which isn't on this branch yet (PowerShell/PowerShell#28410). Its Expand-Archive makes the same calls per entry and also checks a growing array with -notcontains. I can redo this on top of 1.2.7 once it's published.

I used GitHub Copilot to investigate this and to help write the change and tests. I reviewed the code and ran the tests myself.

PR Checklist

  • PR has a meaningful title
  • Summarized changes
  • This PR is ready to merge and is not Work in Progress
  • Tests added
  • Documentation needed. No user-visible change

Expand-Archive spends most of its time on script work per entry rather than
on extracting. For each entry it runs New-Object for a generic List in
Get-WindowsArchiveEntryPathValidationResult and for a FileInfo, calls
Test-Path -PathType Container on the parent folder and trims the path
segments with a ForEach-Object pipeline.

Collect the updated segments with a foreach statement, cast the path to
FileInfo, check the parent folder with [System.IO.Directory]::Exists and
trim the segments with a foreach statement. These all work on PowerShell
3.0, which the manifest still declares.

Add tests that expand an archive with nested folders, empty folder entries,
a zero-byte file, binary content and Unicode names, and check the extracted
files byte for byte and the exact -PassThru output, both for a fresh
expansion and for -Force over an existing one. Also cover a reserved device
name in a nested entry.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Review feedback. Keep the segments in a List[string] created with ::new()
instead of collecting the foreach output into a string array.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SteveL-MSFT
Steve Lee (SteveL-MSFT) merged commit 448033d into PowerShell:servicing Nov 20, 2026
5 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the expand-archive-per-entry-perf branch November 20, 2026 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants